﻿using System;
using System.Web;
using Zhang.IDAL.UCenter.Login;
using Zhang.key;
using Zhang.Model.UCenter;

namespace Zhang.BLL.UCenter.Login
{
    public class Login
    {
        private static readonly ILogin Dal = IDALFactory.UCenterAccess<ILogin>.CreateDal("Login");
        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="id"></param>
        public Model.ResponseMessage Register(Model.UCenter.Login model)
        {
            var msg = new Model.ResponseMessage()
            {
                Status = 200,
                Message = "注册成功"
            };
            try
            {
                #region 判断用户输入内容

                if (string.IsNullOrEmpty(model.VCode) || model.VCode.Trim() == "")
                {
                    msg.Status = 4001;
                    msg.Message = "验证码不能为空";
                    return msg;
                }
                if (HttpContext.Current.Session[SessionKey.UC_USER_LOGIN_VCODE] == null || string.IsNullOrEmpty(model.VCode) || HttpContext.Current.Session[SessionKey.UC_USER_LOGIN_VCODE].ToString().ToLower() != model.VCode.ToLower())
                {
                    msg.Status = 4002;
                    msg.Message = "验证码不正确 ";
                    return msg; // 验证码不正确 
                }

                if (string.IsNullOrEmpty(model.UserName) || model.UserName.Trim() == "")
                {
                    msg.Status = 4003;
                    msg.Message = "账户不能为空";
                    return msg;
                }
                if (string.IsNullOrEmpty(model.Pwd) || model.Pwd.Trim() == "")
                {
                    msg.Status = 4004;
                    msg.Message = "密码不能为空";
                    return msg;
                }
                User modelUser = Dal.GetByKey(model);
                if (modelUser.ID>0)
                {
                    msg.Status = 4011;
                    msg.Message = "用户已存在，请前往登录";
                    return msg;
                }
                var userId= Dal.Register(model);
            

                //HttpContext.Current.Session[SessionKey.UC_USER_INFORMATION] = new User()
                //{

                //    ID = userId,
                //    Mail = model.Mail,
                //    Mobile = model.Mobile,
                //    Pwd = model.Pwd,
                //    UserName = model.UserName
                //};

                #endregion
            }
            catch (Exception e)
            {
                msg.Status = 50000;
                msg.Message = "用户注册请求异常";
                return msg;
            }
           
            return msg;
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public Model.ResponseMessage LoginBySms(Model.UCenter.Login model)
        {
            var msg = new Model.ResponseMessage()
            {
                Status = 200,
                Message = "登录成功"
            };
            try
            {
                #region 判断用户输入内容
                if (string.IsNullOrEmpty(model.VCode) || model.VCode.Trim() == "")
                {
                    msg.Status = 4001;
                    msg.Message = "验证码不能为空";
                    return msg;
                }
                if (HttpContext.Current.Session[SessionKey.UC_USER_LOGIN_VCODE] == null || string.IsNullOrEmpty(model.VCode) || HttpContext.Current.Session[SessionKey.UC_USER_LOGIN_VCODE].ToString().ToLower() != model.VCode.ToLower())
                {
                    msg.Status = 4002;
                    msg.Message = "验证码不正确 ";
                    return msg; // 验证码不正确 
                }

                if (string.IsNullOrEmpty(model.UserName) || model.UserName.Trim() == "")
                {
                    msg.Status = 4003;
                    msg.Message = "账户不能为空";
                    return msg;
                }
                if (string.IsNullOrEmpty(model.Pwd) || model.Pwd.Trim() == "")
                {
                    msg.Status = 4004;
                    msg.Message = "密码不能为空";
                    return msg;
                }
                User modelUser = Dal.GetByKey(model);
                if (modelUser==null || modelUser.ID <= 0)
                {
                    msg.Status = 4005;
                    msg.Message = "账户不存在，请注册";
                    return msg;
                }
                if (modelUser?.Pwd != model.Pwd)
                {
                    msg.Status = 4006;
                    msg.Message = "密码错误";
                    return msg;
                }


                HttpContext.Current.Session[SessionKey.UC_USER_INFORMATION] = new User()
                {
                   
                    ID = modelUser.ID,
                    Mail = model.Mail,
                    Mobile = modelUser.Mobile,
                    Pwd  =modelUser.Pwd,
                    UserName = modelUser.UserName
                };

                #endregion
            }
            catch (Exception e)
            {
                msg.Status = 50000;
                msg.Message = "用户登录请求异常";
                return msg;
            }

            return msg;

        }
    }
}
